﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;
using System.Data.Objects.SqlClient;
using System.Collections;

namespace Hired
{
    /*  A job posting made by a company.
     *  This class contains all the attributes available in a job posting.
     *  At times, a company might want to leave some of them blank.
     *  This is allowed if they are not required.   */
    public class DBJob
    {
        //create job
        public static bool CreateJob(Job j) 
        {
            j.JobID = "tempvalue";
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.AddToJobs(j);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve job
        public static Job RetrieveJob(string retrieveID)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                    return
                    (from j in db.Jobs.Include("CompanyAccount.Company")
                    where j.JobID == retrieveID
                    select j).SingleOrDefault();
            }
        }
        public static List<object> RetrieveJobsByIndustry(string industry)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                var results =
                    from j in db.Jobs
                    
                    where j.Industry == industry
                    select new
                    {
                        Title = j.Title,
                        Description = j.Description,
                        MinimumQualifications = j.MinimumQualifications,
                        EstimatedSalary = j.EstimatedSalary,
                        WorkingHours = j.WorkingHours,
                        Type = j.Type,
                        Timestamp = j.Timestamp,
                        Location = j.Location,
                        Industry = j.Industry,
                        Employer = j.CompanyAccount.Company.CompanyName,
                        JobId = j.JobID,
                        CompanyId = j.CompanyAccount.CAccountID
                    };
                return results.ToList<object>();
                
            }
        }
        
        //retrieveJobByCompany
        public static List<Job> retrieveJobByCompany(string retrieveQueryID) 
        {
            retrieveQueryID = retrieveQueryID.ToUpper();
            using (hireddbEntities db = new hireddbEntities()) 
            {
                var retrieveResults =
                    from j in db.Jobs
                    where j.CAccountID == retrieveQueryID
                    select j;

                return retrieveResults.ToList<Job>();
            }
        }

        //update job
        public static bool UpdateJob(Job j)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Job jobToUpdate = RetrieveJob(j.JobID);
                    db.Attach(jobToUpdate);
                    jobToUpdate.Title = j.Title;
                    jobToUpdate.Description = j.Description;
                    jobToUpdate.MinimumQualifications = j.MinimumQualifications;
                    jobToUpdate.EstimatedSalary = j.EstimatedSalary;
                    jobToUpdate.WorkingHours = j.WorkingHours;
                    jobToUpdate.Type = j.Type;
                    jobToUpdate.Timestamp = j.Timestamp;
                    jobToUpdate.Location = j.Location;
                    jobToUpdate.Industry = j.Industry;

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
            
        }

        //delete job
        public static bool DeleteJob(Job j)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Job deleteJob = RetrieveJob(j.JobID);
                    db.Attach(deleteJob);
                    db.DeleteObject(deleteJob);
                    db.SaveChanges();
                }
                return true;
            }
            catch(Exception) 
            {
                return false;
            }
        }
        public static bool DeleteJobById(string id)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Job deleteJob = RetrieveJob(id);
                    db.Attach(deleteJob);
                    db.DeleteObject(deleteJob);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static List<Job> searchJob(string searchQuery)
        {
            searchQuery = searchQuery.ToUpper();
            using (hireddbEntities db = new hireddbEntities()) {
                var searchResults =
                    from a in db.Jobs
                    where a.Title.ToUpper().Contains(searchQuery)
                    || a.Description.ToUpper().Contains(searchQuery)
                    || a.MinimumQualifications.ToUpper().Contains(searchQuery)
                    || a.Type.ToUpper().Contains(searchQuery)
                    || a.Location.ToUpper().Contains(searchQuery)
                    || a.Industry.ToUpper().Contains(searchQuery)
                    || a.EstimatedSalary.ToUpper().Contains(searchQuery)
                    select a;
                return searchResults.ToList<Job>();
            }
        }
        public static List<Job> advancedSearchJob(List<Job> regularResults, string excludeQuery, string jobLocation, string jobIndustry, int salaryLower, int salaryUpper) {
            excludeQuery = excludeQuery.ToUpper();
            jobIndustry = jobIndustry.ToUpper();
            jobLocation = jobLocation.ToUpper();
            List<Job> advancedSearchResults = regularResults;
            //exclude query
            if (excludeQuery != ""){
                for (int i = 0; i < advancedSearchResults.Count; i++)
                {
                    Job a = advancedSearchResults[i];
                    if (a.Title.ToUpper().Contains(excludeQuery)
                        || a.Description.ToUpper().Contains(excludeQuery)
                        || a.MinimumQualifications.ToUpper().Contains(excludeQuery)
                        || a.Type.ToUpper().Contains(excludeQuery)
                        || a.Location.ToUpper().Contains(excludeQuery)
                        || a.Industry.ToUpper().Contains(excludeQuery)
                        || a.EstimatedSalary.ToUpper().Contains(excludeQuery)
                      )
                    {
                        advancedSearchResults.Remove(a);
                    }
                }
            }
            //industry
            for (int i = 0; i < advancedSearchResults.Count; i++)
            {
                if (jobIndustry != "Select an Industry...".ToUpper())
                {
                    Job a = advancedSearchResults[i];
                    if (!a.Industry.ToUpper().Equals(jobIndustry))
                    {
                        advancedSearchResults.Remove(a);
                    }
                }
            }
            //location
            for(int i =0; i<advancedSearchResults.Count; i++){
                Job a = advancedSearchResults[i];
                if (jobLocation != "Select a Location...".ToUpper())
                {
                    if(!a.Location.ToUpper().Equals(jobLocation)){
                        advancedSearchResults.Remove(a);
                    }
                }
            }
            //salaryMin
            for (int i = 0; i < advancedSearchResults.Count; i++ )
            {
                Job a = advancedSearchResults[i];
                if (salaryLower != 0)
                {
                    if (!(Convert.ToInt32(a.EstimatedSalary) >= salaryLower))
                    {
                        advancedSearchResults.Remove(a);
                    }
                }
            }
            //salaryMax
            for (int i = 0; i < advancedSearchResults.Count; i++ )
            {
                Job a = advancedSearchResults[i];
                if (salaryUpper != 0)
                {
                    if (!(Convert.ToInt32(a.EstimatedSalary) <= salaryUpper))
                    {
                        advancedSearchResults.Remove(a);
                    }
                }
            }            
            return advancedSearchResults;
        }
    }
}
